package com.zerdoor.service.impl;

import com.zerdoor.dao.IFavoriteDao;
import com.zerdoor.service.IFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhongjieyu on 2014/12/26.
 */
@Service
public class FavoriteServiceImpl implements IFavoriteService {

    @Autowired
    private IFavoriteDao favoriteDao;


    @Override
    public String addFavorite(int user_id,int collect_id,int type) throws Exception {

        Map<String,Integer> param = new HashMap<String, Integer>();

        param.put("user_id",user_id);
        param.put("collect_id",collect_id);
        param.put("type",type);

        String favoriteId = favoriteDao.findFavorite(param);

        //如果data不为空表示此条记录已存在，则不在被收藏
        if(favoriteId != null && !"".equals(favoriteId)){
            return favoriteId;
        }


        favoriteDao.addFavorite(param);

        //124101 为作品类型，需要维护作品收藏次数
        if(124101 == type){

            param.put("operate",1);

            favoriteDao.updateWorksFavoriteNumById(param);

        }

        return String.valueOf(param.get("favorite_id"));
    }



    @Override
    public String findFavorite(int user_id,int collect_id,int type) throws Exception {

        Map<String,Integer> param = new HashMap<String, Integer>();

        param.put("user_id",user_id);
        param.put("collect_id",collect_id);
        param.put("type",type);



        return favoriteDao.findFavorite(param);
    }

    @Override
    public void deleteFavorite(int favoriteId,int collectId,int type) throws Exception {


        favoriteDao.deleteFavorite(favoriteId);

        //维护作品收藏数量
        if(124101 == type){

            Map<String,Integer> param = new HashMap<String, Integer>();
            param.put("operate",0);
            param.put("collect_id",collectId);

            favoriteDao.updateWorksFavoriteNumById(param);

        }

    }


}
